from socket import *

# 创建tcp套接字
s = socket()
s.bind(('0.0.0.0', 8000))
s.listen(3)

while True:
    c, addr = s.accept()
    print('Connect from', addr)
    data = c.recv(4096)
    if not data:
        continue
    #     将data按行分割
    request_line = data.decode().splitlines()[0]
    # 获取请求内容
    info = request_line.split(' ')[1]
    print(info)
    # http响应
    # data = """HTTP/1.1 200 OK
    # Content-Ty  pe:text/html
    #
    # <h1>Hello</h1>
    #
    #
    # """

    # 读取html文件，作为响应发送给客户端
    with open('index.html', mode='rb') as html_file:

        # head_row = "HTTP/1.1 200 OK\r\n"
        # head_row += "Content-Type:text/html\r\n"
        # head_row += "\r\n"
        head_row = """HTTP/1.1 200 OK \r\nContent-Type:text/html \r\n\r\n"""

        c.send(head_row.encode())
        while True:
            html_data = html_file.read(1024)
            if not html_data:
                break

            c.send(html_data)
    c.close()
s.close()
